package com.charge.web;

import com.charge.domain.Account;
import com.charge.domain.User;
import com.charge.dto.TransferImportDTO;
import com.charge.dto.TransferRecordDTO;
import com.charge.service.AccountService;
import com.charge.service.TransferRecordService;
import com.charge.util.*;
import com.charge.vo.TransferRecordVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;

/**
 * Description :
 * Author : 91658
 * Date : 2018/6/20 15:57
 **/
@Controller
@RequestMapping("/transfer")
public class TransferController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private TransferRecordService transferRecordService;

    @RequestMapping("/transfer_manage")
    public String transferManage(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Account account = new Account();
        account.setUserId(user.getId());
        List<Account> accountList = accountService.selectAccountList(account);
        request.setAttribute("accountList", accountList);

        return "/transfer/transfer_manage";
    }

    @RequestMapping("/transfer_record")
    @ResponseBody
    public LayResponse<List<TransferRecordVO>> transferRecord(TransferRecordDTO transferRecordDTO, HttpSession session) {
        User user = (User) session.getAttribute("user");
        transferRecordDTO.setUserId(user.getId());
        Integer count = transferRecordService.selectTransferRecordCount(transferRecordDTO);
        Page page = new Page(transferRecordDTO.getPageNo(), transferRecordDTO.getPageSize(), count);
        transferRecordDTO.setBeginRow(page.getBeginRow());
        transferRecordDTO.setEndRow(page.getEndRow());
        List<TransferRecordVO> transferRecordVOList = transferRecordService.selectTransferRecordList(transferRecordDTO);
        return LayResponse.createBySuccess(Const.SUCCESS, count, transferRecordVOList);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public WebResponse<String> upload(HttpServletRequest request) {
        try {
            CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            if (commonsMultipartResolver.isMultipart(request)) {
                MultipartHttpServletRequest multiRequest = commonsMultipartResolver.resolveMultipart(request);
                Iterator<String> fileNames = multiRequest.getFileNames();
                while (fileNames.hasNext()) {
                    // 取得上传文件
                    MultipartFile file = multiRequest.getFile(fileNames.next());
                    if (file != null) {
                        // 取得当前上传文件的文件名称
                        String myFileName = file.getOriginalFilename();
                        // 如果名称不为“”,说明该文件存在，否则说明该文件不存在
                        if (myFileName.trim() != "") {
                            ImportExcel ei =  new ImportExcel(file, 0, 0);
                            List<TransferImportDTO> importList = ei.getDataList(TransferImportDTO.class);
                            if (CollectionUtils.isNotEmpty(importList)) {
                                transferRecordService.importTransferRecord(importList);
                                return WebResponse.createBySuccess(Const.SUCCESS, "");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return WebResponse.createByError(Const.ERROR, "");
        }
        return WebResponse.createByFail(Const.FAIL, "");
    }
}
